package com.zlm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlm.constants.SysConstants;
import com.zlm.domain.ResponseResult;
import com.zlm.domain.entity.Category;
import com.zlm.domain.vo.ArticleDetailVo;
import com.zlm.domain.vo.ArticleListVo;
import com.zlm.domain.vo.HotArticleVo;
import com.zlm.domain.vo.PageVo;
import com.zlm.mapper.ArticleMapper;
import com.zlm.domain.entity.Article;
import com.zlm.service.ArticleService;
import com.zlm.service.CategoryService;
import com.zlm.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文章表(Article)表服务实现类
 *
 * @author makejava
 * @since 2024-12-09 11:10:24
 */
@Service("articleService")
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;

    @Override
    public ResponseResult hotArticleList() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SysConstants.ARTICLE_STATUS_NORMAL);
        queryWrapper.orderByDesc(Article::getViewCount);
        Page<Article> page = new Page<>(1, 10);
        Page<Article> articlePage = page(page, queryWrapper);
        List<Article> articles = articlePage.getRecords();
        List<HotArticleVo> hotArticleVos = BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);
        return ResponseResult.okResult(hotArticleVos);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        //分类id
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();

        articleLambdaQueryWrapper.eq(Objects.nonNull(categoryId)&&categoryId>0,Article::getCategoryId,categoryId);

        //发布状态
        articleLambdaQueryWrapper.eq(Article::getStatus, SysConstants.ARTICLE_STATUS_NORMAL);
        //置顶排序
        articleLambdaQueryWrapper.orderByDesc(Article::getIsTop);
        //分页
        Page<Article> page = new Page<>(pageNum, pageSize);
        Page<Article> articlePage = page(page, articleLambdaQueryWrapper);
        List<Article> articles = articlePage.getRecords();

        //查询分类名称
        articles.forEach(article -> {
                    Category category = categoryService.getById(article.getCategoryId());
                    article.setCategoryName(category.getName());
                });

        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(articles, ArticleListVo.class);
        PageVo<ArticleListVo> pageVo = new PageVo<>(articleListVos,page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getArticleDetail(Long id) {
        Article article = getById(id);
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        Category category = categoryService.getById(articleDetailVo.getCategoryId());
        if(category!=null){
            articleDetailVo.setCategoryName(category.getName());
        }
        return ResponseResult.okResult(articleDetailVo);
    }
}

